package com.beagle.njddapp.ui.activity.oil;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.event.OilChangeEvent;
import com.beagle.njddapp.event.OilOrderEvent;
import com.beagle.njddapp.event.SkEnent;
import com.beagle.njddapp.ui.adapter.OilHomeOrderAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.OilOwnerOrderPresenter;
import com.beagle.njddapp.ui.view.OilOwnerOrderView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.beagle.njddapp.weight.dialog.OilDIalog;
import com.beagle.njddapp.weight.dialog.ShouKuanDIalog;
import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-22 17:36
 */
public class OilOwnerOrderActivity extends BaseMvpActivity<OilOwnerOrderView, OilOwnerOrderPresenter> implements OilOwnerOrderView, RecyclerViewItemClickLisenter<OilHomeBean>, OilHomeOrderAdapter.OilOrderHanldeCallBack, ShouKuanDIalog.skDialogHandleCallback, OilDIalog.OilDialogHandleCallback {

    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.ll_empty)
    View empty;
    @BindView(R.id.common_recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout refreshLayout;
    private OilHomeOrderAdapter adapter;
    private List<OilHomeBean> oilOrderBeanList;
    private OilHomeBean homeBean;
    private ShouKuanDIalog shouKuanDIalog;
    private OilDIalog oilDIalog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_oil_orders;
    }

    @Override
    public void init() {
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        tvTitle.setText("我的订单");
        container.setBackgroundColor(Color.WHITE);
        StatusBarUtil.setPaddingSmart(this, container);
        oilOrderBeanList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new OilHomeOrderAdapter(oilOrderBeanList, this);
        adapter.setOilOrderHanldeCallBack(this);
        adapter.setOilHomeBeanRecyclerViewItemClickLisenter(this);
        recyclerView.setAdapter(adapter);
        presenter.getList();
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                presenter.getList();
            }
        });
    }

    @Override
    public OilOwnerOrderView getMvpView() {
        return this;
    }

    @Override
    public OilOwnerOrderPresenter createPresenter() {
        return new OilOwnerOrderPresenter();
    }

    @Override
    public void showOrderList(List<OilHomeBean> results) {
        refreshLayout.finishRefresh();
        if (results==null||results.size()<1){
            empty.setVisibility(View.VISIBLE);
            return;
        }
        empty.setVisibility(View.GONE);
        oilOrderBeanList.clear();
        oilOrderBeanList.addAll(results);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
        refreshLayout.finishRefresh();
    }

    @Override
    public void onItemClickLisenter(OilHomeBean oilHomeBean) {
        startActivity(new Intent(this, OilOrderDetailActivity.class).putExtra("orderId",oilHomeBean.getOrderId()));

    }

    @OnClick({R.id.common_back_iv})
    public void onMyClick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        presenter.getList();
    }

    @Override
    public void handle(OilHomeBean oilHomeBean) {
        if (oilHomeBean==null){
            return;
        }
        if (oilHomeBean.isPayed()){
            return;
        }
        homeBean=oilHomeBean;
        if ("waiting".equals(oilHomeBean.getOrderState())){
            presenter.confirmed(oilHomeBean.getOrderId());
        }else if ("confirmed".equals(oilHomeBean.getOrderState())){
            presenter.arrived(oilHomeBean.getOrderId());
        }else if ("arrived".equals(oilHomeBean.getOrderState())){
            if(oilDIalog==null){
                oilDIalog=new OilDIalog(this);
                oilDIalog.setOilDialogHandleCallback(this);
            }
            oilDIalog.show();
            oilDIalog.setOrderId(oilHomeBean.getOrderId());
        }else if ("finished".equals(oilHomeBean.getOrderState())){
            if (oilHomeBean.getPaymentType()==5){
                if (shouKuanDIalog==null){
                    shouKuanDIalog=new ShouKuanDIalog(this);
                    shouKuanDIalog.setSkDialogHandleCallback(this);
                }
                shouKuanDIalog.show();
            }
        }
    }

    @Override
    public void showArrived() {
        ToastUtils.showShort("操作成功  请在加油完成后进行完成加油操作");
        if (homeBean!=null){
            homeBean.setOrderState("arrived");
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showSubmit() {
        ToastUtils.showShort("操作成功  请等待订单发起人发起支付");
        if (homeBean!=null){
            EventBus.getDefault().post(new OilChangeEvent(true));
            homeBean.setOrderState("finished");
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void shoukuan() {
        ToastUtils.showShort("收款成功");
        EventBus.getDefault().post(new SkEnent());
        if (homeBean!=null){
            homeBean.setPayed(true);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showJiedan(int orderId) {
        if (homeBean!=null){
            homeBean.setOrderState("confirmed");
            adapter.notifyDataSetChanged();
            List<OilHomeBean> list=new ArrayList<>();
            list.add(homeBean);
            EventBus.getDefault().post(new OilOrderEvent(list));
        }
    }

    @Override
    public void shouKuan(String count, String price) {
        if (homeBean!=null){
            presenter.confirmOrderPayment(homeBean,count,price);
        }
    }

    @Override
    public void oilOk(int orderId, String count) {
        presenter.submit(orderId,count);

    }
}
